//
//  ShopsDetailsViewController.m
//  何五路
//
//  Created by _ADY on 15/7/9.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "ShopsDetailsViewController.h"

@interface ShopsDetailsViewController ()

@end

#define aHight  50
@implementation ShopsDetailsViewController
@synthesize allArray,shopsId;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
}

#pragma mark 分享
-(void)rightAction
{
    
}

#pragma mark 关注
-(void)gzAction
{

}
#pragma mark 查询执照
-(void)zzAction
{
    
}
//@"全部商品",@"上新",@"促销",@"店铺动态"
-(void)aInyBction:(id)sender
{
    //    int topType = ((UIButton*)sender).tag - 10;
}

//@"在线客服",@"商家电话",@"店铺二维码"
-(void)bcAction:(id)sender
{
    //    int topType = ((UIButton*)sender).tag - 20;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.backgroundColor = bgColor;
    self.title = @"店铺详情";
}

#pragma mark -
#pragma mark Table View DataSource

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section == 0)
        return 0.001;
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (allArray.count >0)
        return 3;
    return 0;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 3;
    }
    else if (section == 2) {
        return 4;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
        return 200;

    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
      
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.backgroundColor = [UIColor whiteColor];
      cell.accessoryType = UITableViewCellAccessoryNone;
    if (indexPath.section == 0)
        return [self mCell:cell aCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 1)
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 2)
        return [self mCell:cell cCTableView:aTableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell aCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10,10, 100, 40)];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"Logo"]]]];
    [cell.contentView addSubview:imageView1];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110,10, screenMySize.size.width-15-108/2-100, 30)];
    titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ShopName"]];
    titleLabel.textAlignment = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    titleLabel.textColor = [UIColor blackColor];
    [cell.contentView addSubview:titleLabel];
    
#pragma mark  暂无关注人数接口
    UILabel *title1Label = [[UILabel alloc] initWithFrame:CGRectMake(110,40, screenMySize.size.width-15-108/2-100, 10)];
    title1Label.text = [NSString stringWithFormat:@"%@人关注",[allArray objectForKey:@"ShopFocusCount"]];
    title1Label.textAlignment = 0;
    title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
    title1Label.textColor = [UIColor grayColor];
    [cell.contentView addSubview:title1Label];
    
    UIButton *gzButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [gzButton setFrame:CGRectMake(screenMySize.size.width-15-108/2, 20, 104/2, 48/2)];
    if (([[NSString stringWithFormat:@"%@",[allArray objectForKey:@"IsCustomerFocus"]] isEqualToString:@"1"]))
        [gzButton setImage:[UIImage imageNamed:@"Shopsgzss"] forState:UIControlStateNormal];
    else
        [gzButton setImage:[UIImage imageNamed:@"Shopsgzs"] forState:UIControlStateNormal];
    [gzButton setImage:[UIImage imageNamed:@"Shopsgzss"] forState:UIControlStateHighlighted];
    [gzButton addTarget:self action:@selector(gzAction) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:gzButton];
    
    for (int i = 0; i < 4; i ++)
    {
        if (i != 0)
        {
            UIView *b2View = [[UIView alloc] initWithFrame:CGRectMake(screenMySize.size.width*i/4,60, .5, aHight-20)];
            b2View.backgroundColor = [UIColor colorWithWhite:.8 alpha:1];
            [cell.contentView addSubview:b2View];
        }
        
        
        UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgButton setFrame:CGRectMake(0, 0, screenMySize.size.width/4-2.5, aHight)];
        //        [bgButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:.1]];
        bgButton.center = CGPointMake((screenMySize.size.width+2.5)*i/4+screenMySize.size.width/(4*2),50+aHight/2);
        bgButton.tag = 10+i;
        [bgButton addTarget:self action:@selector(aInyBction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:bgButton];
        
        NSArray *titArray = [NSArray arrayWithObjects:@"全部商品",@"上新",@"促销",@"店铺动态", nil];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, screenMySize.size.width/4-2.5, 30)];
        titleLabel.text = @"0";
        titleLabel.textAlignment = 1;
        //        HelveticaNeue-Bold
        titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
        titleLabel.textColor = [UIColor blackColor];
        [bgButton addSubview:titleLabel];

        if (i == 0) {
            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductTotalCount"]];
        }
        else if (i == 1) {
//            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductTotalCount"]];
        }
        else if (i == 2) {
            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductActivityCount"]];
        }
        else if (i == 3) {
//            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductTotalCount"]];
        }
        UILabel *title1Label = [[UILabel alloc] init];
        title1Label.frame = CGRectMake(0,30, screenMySize.size.width/4-2.5, 20);
        title1Label.text = [titArray objectAtIndex:i];
        title1Label.textAlignment = 1;
        title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
        title1Label.textColor = [UIColor grayColor];
        [bgButton addSubview:title1Label];

    }
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, 90, screenMySize.size.width-20, 15) setTitle:@"........................................................................................................" setColor:NO]];
    
    NSArray *array = [NSArray arrayWithObjects:@"商品评分",@"服务评分",@"时效评分", nil];
    for (int i = 0; i < 3; i++)
    {
        [cell.contentView addSubview:[self setFrame:CGRectMake(10, 110+i*30, 80, 30) setTitle:[array objectAtIndex:i] setColor:YES]];
        NSString *mString = nil;
        if (i == 0)
        {
            mString = [NSString stringWithFormat:@"%.2f分 %@",[[allArray objectForKey:@"ProductScore"] floatValue],[self isIstring:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductScore_Industry"]]]];

        }
        else if (i == 1)
        {
              mString = [NSString stringWithFormat:@"%.2f分 %@",[[allArray objectForKey:@"ServiceScore"] floatValue],[self isIstring:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"ServiceScore_Industry"]]]];
        }
        else if (i == 2)
        {
            mString = [NSString stringWithFormat:@"%.2f分 %@",[[allArray objectForKey:@"TimelyScore"] floatValue],[self isIstring:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"TimelyScore_Industry"]]]];
        }
        [cell.contentView addSubview:[self setFrame:CGRectMake(90, 110+i*30, screenMySize.size.width-100, 30) setTitle:mString]];
    }

    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [NSArray arrayWithObjects:@"在线客服",@"商家电话",@"店铺二维码", nil];
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, 5, 80, 30) setTitle:[array objectAtIndex:indexPath.row] setColor:YES]];
    if (indexPath.row == 1) {
          [cell.contentView addSubview:[self setFrame:CGRectMake(90, 5, screenMySize.size.width-100-21-15, 30) setTitle:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"CustomerServiceTel"]] setColor:NO]];
    }
    
    NSArray *array1 = [NSArray arrayWithObjects:@"Shopslxkf",@"Shopsdh",@"Shopsewm", nil];
    UIButton *gzButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [gzButton setFrame:CGRectMake(screenMySize.size.width-10-21, (40-21)/2, 42/2, 42/2)];
    [gzButton setImage:[UIImage imageNamed:[array1 objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
    gzButton.tag = 20+indexPath.row;
    [gzButton addTarget:self action:@selector(bcAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:gzButton];
    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell cCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSArray *array = [NSArray arrayWithObjects:@"店铺介绍",@"公司名称",@"所在地区",@"开店时间", nil];
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, 5, 80, 30) setTitle:[array objectAtIndex:indexPath.row] setColor:YES]];
    
    if (indexPath.row == 0) {
            [cell.contentView addSubview:[self setFrame:CGRectMake(90, 5, screenMySize.size.width-100, 30) setTitle:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"Description"]] setColor:NO]];
    }
    else if (indexPath.row == 1)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell.contentView addSubview:[self setFrame:CGRectMake(90, 5, screenMySize.size.width-190, 30) setTitle:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"ShopName"]] setColor:NO]];
        
        UIButton *gzButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [gzButton setFrame:CGRectMake(screenMySize.size.width-80-21, 5, 80, 30)];
        [gzButton setTitle:@"查询执照" forState:UIControlStateNormal];
        gzButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
        [gzButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [gzButton addTarget:self action:@selector(zzAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:gzButton];
    }
    else if (indexPath.row == 2)
    {
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(90, 5, screenMySize.size.width-100, 30) setTitle:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"Description"]] setColor:NO]];
    }
    else if (indexPath.row == 3)
    {
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(90, 5, screenMySize.size.width-100, 30) setTitle:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"CreatedOnUtc"]] setColor:NO]];
    }

    return cell;
}

-(NSString*)isIstring:(NSString*)agSring
{
    NSString *stinr = nil;
    BOOL Ds = NO;
    NSRange foundObj=[agSring rangeOfString:@"-" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0)
        Ds = YES;
    NSRange foundObj1=[agSring rangeOfString:@"－" options:NSCaseInsensitiveSearch];
    if(foundObj1.length>0)
        Ds = YES;
    if (Ds) {
        stinr = [NSString stringWithFormat:@"低于同行%.2f％",(float)[agSring floatValue]/100];
    }
    else
        stinr  = [NSString stringWithFormat:@"高于同行%.2f％",(float)[agSring floatValue]/100];
    
    return stinr;
}
-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title
{
    BOOL black = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    //    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
    
    NSRange foundObj=[title rangeOfString:@"低" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0)
        black = YES;
    if (black)
        titleLabel.textColor = [UIColor greenColor];
    else
        titleLabel.textColor = [UIColor redColor];
    
    return titleLabel;
}


-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setColor:(BOOL)black
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
//    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+1];
    if (black)
        titleLabel.textColor = [UIColor blackColor];
    else
        titleLabel.textColor = [UIColor grayColor];
    
    return titleLabel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
